package service;

import beans.User;
import dao.UserDAO;
import model.ApiResultModel;
import model.UserLoginInfoModel;
import utils.AESUtils;
import utils.KeyUtils;
import utils.MD5Utils;

public class UserService {

    public static ApiResultModel login(String account, String pwd) {
        try {
            String originalPwd = AESUtils.decrypt(pwd, KeyUtils.USER_AES_KEY);
            if (!User.isAccountAndPwdLengthLegal(account, originalPwd)) {
                return ApiResultModel.failedWithMessage("用户名或密码长度不合法！");
            }
            User user = UserDAO.ME.getByAccount(account);
            if (user == null || !MD5Utils.md5(originalPwd, KeyUtils.USER_MD5_KEY).equals(user.getPwd())) {
                return ApiResultModel.failedWithMessage("用户名或密码错误！");
            }
            String token = UserDAO.ME.setUserToken(user);
            UserLoginInfoModel userLoginInfoModel = new UserLoginInfoModel();
            user.setPwd("");
            userLoginInfoModel.setUser(user);
            userLoginInfoModel.setToken(token);
            //测试用
            System.out.println("用户 " + user.getAccount() + " 已连接");
            return ApiResultModel.successWithObject(userLoginInfoModel);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResultModel.failedWithMessage("用户名或密码错误！");
        }
    }

    public static ApiResultModel register(String account, String pwd) {
        try {
            String originalPwd = AESUtils.decrypt(pwd, KeyUtils.USER_AES_KEY);
            if (!User.isAccountAndPwdLengthLegal(account, originalPwd)) {
                return ApiResultModel.failedWithMessage("用户名或密码长度不合法！");
            }
            User user = UserDAO.ME.getByAccount(account);
            if (user != null) {
                return ApiResultModel.failedWithMessage("用户名已存在！");
            }
            String md5Pwd;
            md5Pwd = MD5Utils.md5(originalPwd, KeyUtils.USER_MD5_KEY);
            User newUser = new User();
            newUser.setAccount(account);
            newUser.setPwd(md5Pwd);
            long userId = newUser.SaveToDB();
            if (userId > 0) {
                return ApiResultModel.successWithMessage("注册成功！请返回登录界面进行登录");
            }
            return ApiResultModel.failedWithMessage("注册失败！");
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResultModel.failedWithMessage("用户名或密码格式不合法！");
        }
    }

    public static ApiResultModel queryUserName(long query_user) {
        User user = UserDAO.ME.getById(query_user);
        if (user == null) {
            return ApiResultModel.failedWithMessage("查找的用户不存在");
        }
        return ApiResultModel.successWithObject(user.getAccount());
    }

    public static ApiResultModel queryUserId(String account) {
        User user = UserDAO.ME.getByAccount(account);
        if (user == null) {
            return ApiResultModel.failedWithMessage("查找的用户不存在");
        }
        return ApiResultModel.successWithObject(user.getId());
    }
}
